package com.ysd.ziyuanshiyongtongji.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.ysd.ziyuanshiyongtongji.dao.IMemberDao;
import com.ysd.ziyuanshiyongtongji.dto.MemberDTO;
import com.ysd.ziyuanshiyongtongji.model.Member;
import com.ysd.ziyuanshiyongtongji.util.CardUtil;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
/**
 * MemberServiceImpl
 */
@Service
public class MemberServiceImpl implements IMemberService {
    @Resource
    private IMemberDao memberDao;

    @Override
    public Page<Member> findpage(MemberDTO dto) {
        Sort sort = Sort.by(Sort.Order.asc("id"));
        Pageable page = PageRequest.of((dto.getPage()- 1) * dto.getLimit(), dto.getLimit(), sort);
        return memberDao.findAll(getWhereClause(dto), page);
    }

    private Specification<Member> getWhereClause(final MemberDTO dto) {
        return new Specification<Member>() {

            /**
             *
             */


            @Override
            public Predicate toPredicate(Root<Member> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate p = criteriaBuilder.conjunction();
                List<Expression<Boolean>> exList = p.getExpressions();
                if (dto.getCardNo()!=null && !"".equals(dto.getCardNo())) {
                 exList.add(criteriaBuilder.equal(root.<String>get("cardNo"), dto.getCardNo()));                    
                }
                if (dto.getName()!=null &&!"".equals(dto.getName())) {
                    exList.add(criteriaBuilder.like(root.<String>get("name"),"%"+dto.getName()+"%"));   
                }
           
                if(dto.getPhone()!=null&&!"".equals(dto.getPhone())){
                    exList.add(criteriaBuilder.equal(root.<String>get("phone"),dto.getPhone()));
                }
                if(dto.getStartDate()!=null){
                    exList.add(criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("regdate"),dto.getStartDate()));
                }
                if(dto.getEndDate()!=null){
                    exList.add(criteriaBuilder.lessThanOrEqualTo(root.<Date>get("regdate"),dto.getEndDate()));
                }
                if(dto.getStatus()!=null){
                    exList.add(criteriaBuilder.equal(root.<Integer>get("status"), dto.getStatus()));
                }
                if(dto.getCategory()!=null&& !"".equals(dto.getCategory())){
                    exList.add(criteriaBuilder.equal(root.<String>get("category"), dto.getCategory()));
                }
                    if(dto.getDepartment()!=null&&!"".equals(dto.getDepartment())){
                        exList.add(criteriaBuilder.equal(root.<String>get("department"), dto.getDepartment()));
                    }
                    if(dto.getProfession()!=null&&!"".equals(dto.getProfession())){
                        exList.add(criteriaBuilder.equal(root.<String>get("profession"), dto.getProfession()));
                    }
                    if(dto.getGrade()!=null&&!"".equals(dto.getGrade())){
                        exList.add(criteriaBuilder.equal(root.<String>get("grade"), dto.getGrade()));
                    }
                
               
                return p;
            }
        };
    }
    @Override
	public Member saveMember(Member m) {
	m.setCardNo(CardUtil.getCardNo());
		return memberDao.save(m);
	}

	@Override
	public void del(Integer id) {
	
	   memberDao.deleteById(id);
	}
    @Override
    public int addAll(List<Member> list){
     List<Member> memberList= memberDao.saveAll(list);
     return memberList.size();
    }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    @Override
    public void delAll(List<Member> list) {
      memberDao.deleteInBatch(list);

    }

    @Override
    public void deletebatch(List<Integer> ids) {
    memberDao.deletebatch(ids);
    } 
    
}